﻿Public Class Worktimevb

    Public u_id As Int16 = 1
    Public emp_id As Int16 = 1
    Public emp_name As String

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lbl24h.Text = TimeOfDay.ToString("HH:mm:ss")
        lbl12h.Text = TimeOfDay
        lblday.Text = Now.ToString("dddd-dd-MMMM-yyyy")
    End Sub

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
        System.Diagnostics.Process.Start("http://khmerbamboo.wordpress.com/")
    End Sub
    Private Sub Worktimevb_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lbl24h.Text = TimeOfDay.ToString("HH:mm:ss")
        lbltmi.Text = ""
        lbltmo.Text = ""
        lbltai.Text = ""
        lbltao.Text = ""
        lblymi.Text = ""
        lblymo.Text = ""
        lblyai.Text = ""
        lblyao.Text = ""
        lblemp.Text = ""
        lblemp.Text = emp_name
        showyesterday()
        showtoday()
        loadot()
        btnin.Enabled = False
        btnout.Enabled = False
        If lbltmi.Text = "" Or lbltai.Text = "" Then
            btnin.Enabled = True
        Else
            btnout.Enabled = True
        End If
        'If lbltmi.Text <> "A" Then
        '    If lbltmo.Text = "" Then
        '        btnout.Enabled = False
        '        btnin.Enabled = True
        '    End If
        'End If
        If lbltmi.Text <> "" And lbltmo.Text <> "" And lbltai.Text <> "" And lbltao.Text <> "" Then
            btnin.Enabled = False
            btnout.Enabled = False
        End If
        If lbl24h.Text >= "17:00:00" Then
            btnin.Enabled = False
        End If
    End Sub

    Private Sub loadot()
        cmd.Parameters("@date") = Now.Date
        cmd.Parameters("@id") = emp_id
        Dim a = cmd.ExecuteScalar("SELECT COUNT(*) FROM overtime_tbl WHERE ot_date=@date AND emp_id=@id")
        'MessageBox.Show(a)
        If a > 0 Then
            lblover.Visible = True
        Else
            lblover.Visible = False
        End If
    End Sub

    Private Sub frmworktime_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Login2.Close()
    End Sub
    Private Sub showyesterday()
        Dim cmd As New SqlClient.SqlCommand("select top(1) * from worktime_tbl where emp_id='" & emp_id & "' and wt_date <> '" & Now.ToString("yyyy-MM-dd") & "' order by wt_id DESC", action.GetConnection)
        Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(dr)
        dr.Close()
        If (dt.Rows.Count > 0) Then
            lblymi.Text = dt.Rows(0).Item(3).ToString
            lblymo.Text = dt.Rows(0).Item(4).ToString
            lblyai.Text = dt.Rows(0).Item(5).ToString
            lblyao.Text = dt.Rows(0).Item(6).ToString
            If lblymi.Text = lblymo.Text And lblymi.Text = lblyai.Text Then
                lblymi.Text = "A"
                lblymo.Text = "A"
            End If
            If lblyao.Text = "" Then
                lblyao.Text = "Forget"
            End If
        End If

    End Sub

    Private Sub showtoday()
        Dim cmd As New SqlClient.SqlCommand("select top(1) * from worktime_tbl where emp_id='" & emp_id & "' and convert(varchar(10), wt_date, 102) = convert(varchar(10), getdate(), 102) order by wt_id DESC", action.GetConnection)
        Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(dr)
        dr.Close()
        If (dt.Rows.Count > 0) Then
            lbltmi.Text = dt.Rows(0).Item(3).ToString
            lbltmo.Text = dt.Rows(0).Item(4).ToString
            lbltai.Text = dt.Rows(0).Item(5).ToString
            lbltao.Text = dt.Rows(0).Item(6).ToString
            If lbltmi.Text = lbltmo.Text And lbltmi.Text = lbltmo.Text Then
                lbltmi.Text = "A"
                lbltmo.Text = "A"
            End If
        End If

    End Sub

    Private Sub btnin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnin.Click

        If lbl24h.Text = "" Then
            MessageBox.Show("Please wait 3 Second")
            Exit Sub
        End If

        cmd.Parameters("@date") = Now.ToString("yyyy-MM-dd")
        cmd.Parameters("@empid") = emp_id
        cmd.Parameters("@time") = lbl24h.Text

        If lbltmi.Text = "" And lbltmo.Text = "" And lbl24h.Text < "10:00:00" Then
            cmd.ExecuteNonQuery("INSERT INTO worktime_tbl values(@empid,@date,@time,null,null,null)")
        ElseIf lbltmi.Text = "" And lbltmo.Text = "" And lbl24h.Text > "12:30:00" Then
            cmd.ExecuteNonQuery("INSERT INTO worktime_tbl values(@empid,@date,@time,@time,@time,null)")
        Else
            cmd.ExecuteNonQuery("UPDATE worktime_tbl SET afternoon_in=@time where emp_id=@empid and convert(varchar(10), wt_date, 102) = convert(varchar(10), getdate(), 102)")
        End If
        showtoday()
        btnout.Enabled = True
        btnin.Enabled = False
        If lbltmi.Text = "" And lbltmo.Text = "" And lbltai.Text = "" Then
            btnout.Enabled = False
            MessageBox.Show("Please wait untill 12:30 PM")
        End If
        'MessageBox.Show(Now.ToString("dd-MM-yyyy"))
    End Sub

    Private Sub btnout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnout.Click

        If lbl24h.Text = "" Then
            MessageBox.Show("Please wait 3 Second")
            Exit Sub
        End If

        cmd.Parameters("@empid") = emp_id
        cmd.Parameters("@time") = lbl24h.Text

        If lbltmo.Text = "" And lbltao.Text = "" Then
            cmd.ExecuteNonQuery("UPDATE worktime_tbl SET morning_out=@time where emp_id=@empid and convert(varchar(10), wt_date, 102) = convert(varchar(10), getdate(), 102)")
        Else
            cmd.ExecuteNonQuery("UPDATE worktime_tbl SET afternoon_out=@time where emp_id=@empid and convert(varchar(10), wt_date, 102) = convert(varchar(10), getdate(), 102)")
        End If
        showtoday()
        btnin.Enabled = True
        If lbltao.Text <> "" Then
            btnin.Enabled = False
        End If
        btnout.Enabled = False
    End Sub


    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblover.Click
        Dim frm As New OvertimeAdd
        frm.ShowDialog()
    End Sub
End Class